from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, JsonResponse
from Applications.IT.models import IT
from Django_Admin.settings import SITE_URL, IMAGE_URL, VUE_URL
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

# Create your views here.

# TODO 返回Json文章列表

class ListAPI(View):

    def get(self, request):
        """处理GET请求业务逻辑"""
        # 通过django的view实现商品列表页
        json_list = []
        # 获取所有商品
        items = IT.objects.all()
        if len(items) == 0:
            return JsonResponse({'status': 0})
        status = 1
        for item in items:
            json_dict = {}
            # 获取商品的每个字段，键值对形式
            if status == 1:
                status = 0
            elif status == 0:
                status = 1

            if item.intro == 'None':
                json_dict['intro'] = item.body[0:50]
            else:
                json_dict['intro'] = item.intro

            time = str(item.pub_time)[0:10]
            json_dict['time'] = time

            json_dict['id'] = str(item.id)
            json_dict['title'] = item.title

            json_dict['type'] = item.type
            json_dict['view'] = str(item.views)
            json_dict['like'] = str(item.like)
            json_dict['status'] = str(status)

            # 合成地址页面
            # href = VUE_URL + "Details/IT/" + str(item.id)
            href = str(item.id)
            json_dict['url'] = href

            # 封面为avatar
            avatar = item.avatar
            avatar_url = IMAGE_URL + str(avatar)
            json_dict['avatar'] = 'url(' + avatar_url +')'


            json_list.append(json_dict)
        # 将获取到的数据返回到 json中
        # 返回json，一定要指定类型content_type='application/json'
        return JsonResponse(json_list, safe=False)
        # return render(request, 'items-classify.html', context=json_list)

    def post(self, request):
        pass



# TODO 返回Json文章内容
@method_decorator(csrf_exempt, name='dispatch')

class ContentAPI(View):

    def get(self, request, ID):
        item = IT.objects.get(id=ID)
        items = IT.objects.all()
        if len(items) == 0:
            return JsonResponse({'status': 0})

        item.views = item.views + 1

        json_dict = {}
        # 获取商品的每个字段，键值对形式

        #取出所有标签
        tagsList = []
        tags = item.tags.all()
        for tag in tags:
            tag = str(tag)
            tagsList.append(tag)

        json_dict['tags'] = tagsList


        time = str(item.pub_time)[0:10]
        json_dict['time'] = time

        json_dict['id'] = str(item.id)
        json_dict['title'] = item.title
        json_dict['body'] = item.body
        json_dict['type'] = item.type
        json_dict['author'] = str(item.author)
        json_dict['view'] = str(item.views)
        json_dict['like'] = str(item.like)

        # 合成地址页面
        href = SITE_URL + "API/IT/content/" + str(item.id)
        json_dict['url'] = href

        # 上一页
        try:
            next = IT.objects.filter(id__gt=item.id).order_by('id').first()
            json_dict['next'] = str(next.id)
        except:
            json_dict['next'] = 'None'
        # 下一页
        try:
            prev = IT.objects.filter(id__lt=item.id).first()
            json_dict['prev'] = str(prev.id)
        except:
            json_dict['prev'] = 'None'

        # 封面为avatar
        avatar = item.avatar
        avatar_url = IMAGE_URL + str(avatar)
        json_dict['avatar'] = 'url(' + avatar_url + ')'
        # 将获取到的数据返回到 json中
        # 返回json，一定要指定类型content_type='application/json'
        item.save()
        return JsonResponse(json_dict, safe=False)
        # return render(request, 'items-classify.html', context=json_list)

    def post(self, request, ID):
        item = IT.objects.get(id=ID)
        item.like += 1
        item.save()
        return JsonResponse({'status': 200})



# TODO 点赞
class FavorAPI(View):

    def get(self, request, ID):
        item = IT.objects.get(id=ID)
        item.like += 1
        print(item.like)
        item.save()
        return JsonResponse({'status': 200})

    def post(self, request):
        pass